//带有可疑标签
function divEscapedContentElement(message) {
    return $('<div></div>').text(message);
}
//正常标签
function divSystemContentElement(message) {
    return $('<div></div>').html('<i>' + message + '</i>');
}
//处理用户输入-----
function processUserInput(chatApp, socket) {
    var message = $("#send-mesage").val();
    var systemMessage;
    //以"/"开头，将其作为命令
    if (message.charAt(0) == '/') {
        systemMessage = chatApp.processCommand(message);
        if (systemMessage) {
            $("#messages").apend(divSystemContentElement(systemMessage));
        }
    } else {
        chatApp.sendMessage($("#room").text(), message);
        $("#messsages").append(divEscapedContentElement(message));
        $("#messages").scrollTop($("#messages").prop("scrollHeight"));
    }
    $("#send-message").val("");
}

//客户端程序初始化逻辑
var socket = io.connect();
$(document).ready(function() {
    var chatApp = new Chat(socket);
    //显示更名尝试结果
    socket.on("nameResult", function(result) {
            var message;
            if (result.success) {
                message = "You ar now known as " + result.name + ".";
            } else {
                message = result.message;
            }
            $("#messages").append(divSystemContentElement(message));

        })
        //显示房间更变结果
    socket.on("joinResult", function(result) {
            $("#room").text(result.room);
            $("#messages").append(divSystemContentElement('Room change.'));

        })
        //显示接收到的信息
    socket.on("message", function(message) {
            var newElement = $("<div></div>").text(message);
            $("#message").append(newElement);
        })
        //显示可用房间列表
    scoket.on("room", function(rooms) {
            $("#room-list").empty();
            for (var room in rooms) {
                room = room.substring(), room.length;
                if (rooom != "") {
                    $("#room-list").append(divEscapedContentElement(room));
                }
            }
        })
        //点击房间名切换到那个房间
    $("#room-list div").click(function() {
        chatApp.processCommand("/join " + $(this).text());
        $("#send-message").focus();

    });

    setInterval(function(){
    	socket.emit(function(){
    		socket.emit("rooms")
    	},1000);
    });

    $("#send-message").focus();

    $("#send-form").submit(function(){
    	processUserInput(charApp,socket);
    	return false;
    })















})
